home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 27 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.1 KB

  1. Path: in2.uu.net!bounce-back
  2. Date: 10 Jan 96 23:08:30 GMT
  3. Approved: fjh@cs.mu.oz.au
  4. From: clamage@Eng.Sun.COM (Steve Clamage)
  5. Newsgroups: comp.std.c++
  6. Subject: Re: Q: returning object from function
  7. X-Original-Date: 10 Jan 1996 16:09:16 GMT
  8. Organization: Sun Microsystems Inc.
  9. Message-ID: <4d0obc$il8@engnews1.Eng.Sun.COM>
  10. References: <4cvsq0$5ig@dub-news-svc-4.compuserve.com>
  11. Reply-To: clamage@Eng.Sun.COM
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMPRG/+EDnX0m9pzZAQELXwF/Qn9/sF4tOWwhq/069q4AFYnkrVYyUOcH
  14.     cGIDHoAK1gHPgbG/ctkVtlD77HQ9990c
  15.     =WQuk
  16.  
  17. In article 5ig@dub-news-svc-4.compuserve.com,
  18. 100754.2730@compuserve.com (Martin Aupperle) writes:
  19. >I have 
  20.  
  21. >  X  x = f();
  22.  
  23. >with some non-trivial class X. If I understand the DWP (12.2:
  24. >temporary objects) right, it is implementation dependent whether 
  25. >- a temporary is constructed (on the stack) that is used to initialize
  26. >x
  27. >- the result is constructed in x's memory directly. 
  28.  
  29. >The difference is the construction of the temporary. If my observation
  30. >is true, there a strong requirement concerning the behaviour of the
  31. >copy constructor, if one wants to write portable software.  
  32.  
  33. >Is that true? 
  34.  
  35. In many kinds of expressions, the compiler is allowed to introduce or
  36. to optimize away the creation and deletion of a temporary variable.
  37. Your example is one of those cases. Another is
  38.     X x = y;
  39. which literally means
  40.     X x(X(y));
  41. but which may be interpreted as
  42.     X x(y);
  43.  
  44. In such cases, the rules of C++ say in effect that the semantics of the
  45. program are the same whether the temporary is created or not. Thus, you
  46. have to design your classes so that program correctness does not depend
  47. on whether extra temporaries are created. 
  48.  
  49. Usually that means that constructors and destructors should have
  50. complementary side effects, and you have to be careful about acquiring
  51. locks or other scarce resources in a copy constructor.
  52.  
  53. ---
  54. Steve Clamage, stephen.clamage@eng.sun.com
  55. ---
  56. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  57.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  58.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  59.